package com.situ.mall.controller.back;

import java.util.List;

import org.aspectj.internal.lang.annotation.ajcDeclareAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

import com.situ.mall.common.ServerResponse;
import com.situ.mall.entity.Product;
import com.situ.mall.service.IProductService;

@RequestMapping("/product")
@Controller
public class ProductController {
	@Autowired
	private IProductService productService;

	// 跳转到商品展示界面
	@RequestMapping("/getProductPage")
	public String getProductPage() {
		return "/product/product_list";
	}

	@RequestMapping("/list")
	@ResponseBody
	public List<Product> list() {
		return productService.list();
	}

	@RequestMapping("/pageList")
	@ResponseBody
	public ServerResponse pageList(Integer page, Integer limit) {
		ServerResponse serverResponse = productService.pageList(page, limit);
		System.out.println(serverResponse);
		return serverResponse;
	}

	@RequestMapping("/deleteById")
	@ResponseBody
	public ServerResponse deleteById(Integer id) {
		return productService.deleteById(id);
	}

	@RequestMapping("/deleteAll")
	@ResponseBody
	public ServerResponse deleteAll(String ids) {
		return productService.deleteAll(ids);
	}

	// 跳转到商品添加界面
	@RequestMapping("/getAddPage")
	public String getAddPage() {
		return "/product/product_add";
	}

	// 添加功能
	@RequestMapping("/add")
	@ResponseBody
	public ServerResponse insertSelective(Product product) {
		System.out.println(product);
		return productService.insertSelective(product);
	}
	//跳转到修改界面
	@RequestMapping("/toupdate")
	public String toupdate(Integer id ,Model model) {
		//先通过ID获取本行的所有信息
		Product product = productService.selectByPrimaryKey(id);
		System.out.println("product = "+product);
		model.addAttribute("product",product);
		return "/product/product_update";
	}
	//修改
	@RequestMapping("/update")
	@ResponseBody
	public ServerResponse update(Product record) {
		return productService.updateByPrimaryKeySelective(record);
	}

}
